/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package contest.util;

import java.io.UnsupportedEncodingException;

/**
 *
 * @author Admin
 */
public class StringUtil {

    public static String trim(String text) {
        if (text != null) {
            return text.trim();
        }
        return text;
    }

    public static String convertNullToEmpty(String text) {
        if (text == null) {
            return "";
        }
        return text;
    }

    public static String convertToUTF8(String a) throws UnsupportedEncodingException {
        if (a != null && !a.equals("")) {

            byte[] b = a.getBytes("ISO-8859-1");
            String c = new String(b, "UTF-8");
            return c;

        }
        return a;
    }

    public static String convertToISO(String a) throws UnsupportedEncodingException {
        if (a != null && !a.equals("")) {
            byte[] b = a.getBytes("UTF-8");
            String c = new String(b, "ISO-8859-1");
            return c;
        }
        return a;
    }

    public static boolean validatePositiveNumber(String number) {
        String temp = "0123456789";
        int i;
        for (i = 0; i < number.length(); i++) {
            int j;
            for (j = 0; j < temp.length(); j++) {
                if (temp.charAt(j) == number.charAt(i)) {
                    j = temp.length();
                }
            }

            if (j != temp.length() + 1) {
                i = number.length();
            }
        }
        if (i == number.length() + 1) {
            return false;
        }
        return true;
    }

    public static String subString(String str, int index, int indexEnd) {
        return str.substring(index, indexEnd);
    }

    public static String splitWord(String str, int length) {
        if (str != null && !str.equals("")) {
            str = StringUtil.trim(str);
            str = StringUtil.trimLeft(str);
            int index = 0;
            int i = 0;
            while (index < length) {
                while (i < str.length() && str.charAt(i) != ' ') {
                    i++;
                }
                index++;
                while (i < str.length() && str.charAt(i) == ' ') {
                    i++;
                }

            }
            str = StringUtil.subString(str, 0, i);
        }
        return str;
    }

    public static String trimLeft(String str) {
        if (str != null && !str.equals("")) {
            int i = 0;
            while (i < str.length() && str.charAt(i) == ' ') {
                i++;
            }
            str = StringUtil.subString(str, i, str.length());
        }
        return str;
    }

    public static String removeHTML(String str) {
        return str.replaceAll("\\<.*?>", "");
    }

    public static int calTotalPage(int totalRecord, int totalRecoredShow) {
        int totalPage = 0;
        if (totalRecord % totalRecoredShow != 0) {
            totalPage = totalRecord / totalRecoredShow + 1;
        } else {
            totalPage = totalRecord / totalRecoredShow;
        }
        return totalPage;
    }

    public static int parseInt(String number) {
        if (number != null) {
            return Integer.parseInt(number);
        }
        return 0;
    }
}
